package TCP;

import java.io.DataInput;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(8888);

        //等待接收客户端，创建管道
        Socket socket = serverSocket.accept();

        //创建完管道之后，就要创建字节流
        InputStream inputStream = socket.getInputStream();
        //创建字节流来获取传进来的字节流
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        while (true) {
            try {
                //获取数据
                String s = dataInputStream.readUTF();
                System.out.println(s+" ，该数据来自IP号为"+socket.getInetAddress().getHostAddress()+" ,来自端口号为:" + socket.getPort());
            } catch (IOException e) {
                String m = socket.getRemoteSocketAddress().toString();
                m += " 离线了";
                System.out.println(m);
                dataInputStream.close();
                socket.close();
                break;
            }
        }


    }
}
